/**Created by the LayaAirIDE*/
module view{
	export class Game extends ui.GameUI{
		//滚动背景
		private bg : BackGround;
		//鱼的容器
    	private monsterBox : Laya.Sprite;
		//鱼钩
		private hook : Laya.Image;
		//鱼线
		private line : Laya.Sprite;
		//鱼钩数量
		private hookNum : number;
		//本次钓到的鱼
		private getFishs : number[];

		constructor(){
			super();

			//创建循环滚动的背景
			this.bg = new BackGround();
			this.addChild(this.bg);
			//实例化鱼的容器
        	this.monsterBox = new Laya.Sprite();
        	this.addChild(this.monsterBox);
			//实例化鱼钩、鱼线
			this.hook = new Laya.Image();
			this.hook.loadImage("ui/hook.png", 0, 0, 30, 50);
			this.hook.pivotX = 10;
			this.hook.pivotY = 20;
			this.addChild(this.hook);
			this.line = new Laya.Sprite();
			this.addChild(this.line);

			this.Init();
		}

		Init() : void {
			let hookX = Math.floor(Laya.stage.width/2);
			let hookY = Math.floor(Laya.stage.height/2);
			this.hook.pos(hookX-15, hookY-25);
			this.line.graphics.drawLine(hookX, 0, hookX+17, hookY-38, "#AAAAAA", 2);

			this.hookNum = 1;
			this.getFishs = [];
			
		}

		Start(lineLen:number, hookNum:number) : void {
			this.hookNum = hookNum;
			this.getFishs = [];

        	Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this.OnMouseMove);
			this.bg.Init(lineLen);
			Laya.timer.loop(ComCfg.LoopTime, this, this.OnLoop);
		}

		End() : void {
			//停止循环
			Laya.timer.clear(this, this.OnLoop);
			//移除舞台的鼠标移动事件
        	Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.OnMouseMove);
			//回收所有的游动的鱼
			for (let i:number =this.monsterBox.numChildren-1; i >= 0; i--) {
				let curMonster : Monster = this.monsterBox.getChildAt(i) as Monster;
				if(curMonster){
					curMonster.removeSelf();
					Laya.Pool.recover("monster", curMonster);
				}
			}
			//移除鱼钩上的鱼
			for (let i:number =this.hook.numChildren-1; i >= 0; i--) {
				let hookFish : Laya.Sprite = this.hook.getChildAt(i) as Laya.Sprite;
				if(hookFish){
					hookFish.removeSelf();
					hookFish = null;
				}
			}
			//移除钓鱼页面
			this.removeSelf();
			//显示开始页面
			Laya.stage.addChild(GameMain.viewGameStart);
			GameMain.viewGameStart.FinishOnceGame(this.getFishs);
		}

		OnMouseMove() : void {
			//鱼钩、鱼线与鼠标的x坐标保持一致
			this.hook.x = Laya.stage.mouseX;
			let hookX = Math.floor(Laya.stage.width/2);
			let hookY = Math.floor(Laya.stage.height/2);
			this.line.graphics.clear();
			this.line.graphics.drawLine(this.hook.x+17, 0, this.hook.x+17, hookY-38, "#AAAAAA", 2);
    	}

		OnLoop() :void {
			//背景滚动
			this.bg.OnLoop();

			//创建鱼
			if(this.bg.curOffset >= ComCfg.CreateFishOffset){
				this.bg.curOffset = 0;
				let curMonsterType = ComFun.GetRandomInt(1, ComCfg.MaxFishType);
				let newMonster : Monster = Laya.Pool.getItemByClass("monster", Monster);
				newMonster.Init(curMonsterType, this.bg.isMoveUp);
				this.monsterBox.addChild(newMonster);
			}

			//移动鱼
			for (let i:number =this.monsterBox.numChildren-1; i >= 0; i--) {
				let curMonster : Monster = this.monsterBox.getChildAt(i) as Monster;
				if(curMonster){
					//移动
					curMonster.Move(this.bg.isMoveUp);
					//检测并回收鱼
					if(curMonster.y < -100 || curMonster.y > Laya.stage.height+100){
						curMonster.removeSelf();
						Laya.Pool.recover("monster", curMonster);
					}
				}
			}

			//鱼钩上升时检测碰撞
			if(this.bg.isMoveUp && this.getFishs.length < this.hookNum){
				for (let i:number =this.monsterBox.numChildren-1; i >= 0; i--) {
					let curMonster : Monster = this.monsterBox.getChildAt(i) as Monster;
					if(curMonster){
						let collideX = ComCfg.HookRadiusX + curMonster.collideRadius;
						let collideY = ComCfg.HookRadiusY + curMonster.collideRadius;
						if(Math.abs(this.hook.x - curMonster.x) < collideX && Math.abs(this.hook.y - curMonster.y) < collideY){
							//添加一条鱼到鱼钩
							let hookFish : Laya.Sprite = new Laya.Sprite();
							let strUrl = "ui/fishh" + curMonster.type+".png";
							hookFish.loadImage(strUrl, 2, 35, 30, 50);
							hookFish.pivotX = 0;
							hookFish.pivotY = 0;
							let randRotation = ComFun.GetRandomInt(0, 40) - 20;
							hookFish.rotation = randRotation;
							this.hook.addChild(hookFish);
							//添加到钓到的鱼的数组
							this.getFishs.push(curMonster.type);
							//回收鱼
							curMonster.removeSelf();
							Laya.Pool.recover("monster", curMonster);
							//检测鱼钩是否已经用完
							if(this.getFishs.length >= this.hookNum){
								//如果鱼钩已经用完，则加速滚动背景
								this.bg.Quick();
							}
						}
					}
				}
			}
		}
	}
}